﻿using System.Collections.Generic;
using LB.Common;

namespace LB.Server.Core
{
    public static class EntitiesConverter
    {
        #region To LB.Common.Entities

        public static LB.Common.User ToCommonUser(this DAL.DomainEntitites.User dalUser)
        {
            return new LB.Common.User
                             {
                                 Login = dalUser.Login,
                                 PasswordHash = dalUser.PasswordHash,
                                 UserId = dalUser.UserId
                             };
        }

        public static LB.Common.Book ToCommonBook(this LB.Server.DAL.DomainEntitites.Book dalBook)
        {
            return new LB.Common.Book
            {
                AuthorId = dalBook.AuthorId,
                BookId = dalBook.BookId,
                Title = dalBook.Title
            };
        }

        public static LB.Common.Chapter ToCommonChapter(this LB.Server.DAL.DomainEntitites.Chapter dalChapter)
        {
            var result = new LB.Common.Chapter()
                       {
                           Author = dalChapter.Creator,
                           BookId = dalChapter.BookId,
                           ChapterId = dalChapter.ChapterId,
                           Title = dalChapter.ChapterTitle,
                           SubChapters = new List<Chapter>()
                       };
            dalChapter.SubChapters.ForEach(c=> result.SubChapters.Add(c.ToCommonChapter()));
            return result;
        }

        #endregion

        #region To DAL Entities

        public static DAL.DomainEntitites.Book ToDalBook(this LB.Common.Book book)
        {
            return new DAL.DomainEntitites.Book
            {
                AuthorId = book.AuthorId,
                BookId = book.BookId,
                Title = book.Title
            };
        }

        public static DAL.DomainEntitites.Chapter ToDalCahpter(this LB.Common.Chapter chapter)
        {
            var result = new DAL.DomainEntitites.Chapter()
                       {
                           ChapterId = chapter.ChapterId,
                           ChapterTitle = chapter.Title,
                           Creator = chapter.Author,
                           BookId = chapter.BookId,
                           SubChapters = new List<DAL.DomainEntitites.Chapter>()
                       };
            chapter.SubChapters.ForEach(c=> result.SubChapters.Add(c.ToDalCahpter()));
            return result;
        }

        #endregion

    }
}
